﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;

namespace SBPweb.Communication.Client.Library.Xmpp.Configuration
{

    public abstract class ConfigurationParserBase
    {

        #region Private fields

        private IDictionary<string, string> parameters;

        #endregion

        #region Constructor logic

        public ConfigurationParserBase(IDictionary<string, string> parameters)
        {
            this.parameters = parameters;
        }

        #endregion

        #region Property accessors

        protected abstract string Prefix
        {
            get;
        }

        #endregion

        #region Methods

        protected virtual string GetValue(string key)
        {
            return parameters[Prefix + "." + key];
        }

        public T GetValue<T>(string key)
        {
            return GetValue<T>(key, default(T));
        }

        public T GetValue<T>(string key, T defaultValue)
        {
            try
            {
                return (T)Convert.ChangeType(GetValue(key), typeof(T), CultureInfo.InvariantCulture);
            }
            catch
            {
                return defaultValue;
            }
        }

        #endregion

    }

}